home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1992 June: ROMin Holiday / ADC Developer CD (1992-06) (''ROMin Holiday'')_iso / Developer Connection - 06-1992.iso / Periodicals / develop / develop 10 code / GWorld Drawing / GWorld Routines / FourBitScaler.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-08  |  5.5 KB  |  147 lines  |  [TEXT/KAHL]

  1. #include "DemoRoutines.h"
  2.  
  3. /* Routine to take a 1-bit offscreen and map it to a 4-bit offscreen 4x smaller */
  4.  
  5. void Scale1BitTo4Bit( GWorldPtr src, GWorldPtr dst )
  6. {
  7.     PixMapHandle    srcPixMap;
  8.     PixMapHandle    dstPixMap;
  9.     short            srcRowBytes;
  10.     short            dstRowBytes;
  11.     long            *srcBaseAddr;
  12.     long            *srcAddr1;
  13.     long            *srcAddr2;
  14.     long            *srcAddr3;
  15.     long            *srcAddr4;
  16.     long            *dstBaseAddr;
  17.     long            *dstAddr;
  18.     long            thirtyTwoPixels1;
  19.     long            thirtyTwoPixels2;
  20.     long            thirtyTwoPixels3;
  21.     long            thirtyTwoPixels4;
  22.     short            mmuMode;
  23.     short            row, column;
  24.     short            TranslationTable[256];
  25.     unsigned        RemapTable[0x211];
  26.     short            dstTenBits;
  27.     unsigned char    dstChar;
  28.     unsigned long    dstLong;
  29.     short            width, height;
  30.     short            index, index1;
  31.     
  32.     for( index = 0; index < 256; index++ )
  33.     {
  34. /*
  35. **    The Translation table takes an index and counts the number of bits set
  36. **
  37. **    TranslationTable format:    Low 5 bits contain number of bits set in low nibble of index
  38. **                                Next 5 bits contain number of bits set in high nibble
  39. **                                Top 6 bits always zero
  40. **
  41. */
  42.         TranslationTable[index] =     ((index & 1) != 0) +
  43.                                     ((index & 2) != 0) +
  44.                                     ((index & 4) != 0) +
  45.                                     ((index & 8) != 0) +
  46.                                     0x20 * ((index & 0x10) != 0) +
  47.                                     0x20 * ((index & 0x20) != 0) +
  48.                                     0x20 * ((index & 0x40) != 0) +
  49.                                     0x20 * ((index & 0x80) != 0);
  50.     }
  51. /*
  52. **        The RemapTable converts a ten bit number into an 8-bit value
  53. **        The ten bit number is a composite of two five bit numbers which can have
  54. **        values from 0-16.  The result for each 5-bit input is:
  55. **
  56. **        0-7     ->     0-7
  57. **        8        ->     7
  58. **        9-$10     ->    8-$F
  59. */
  60.  
  61.     for( index = 0; index <= 0x10; index++ )
  62.     {
  63.         for( index1 = 0; index1 <= 0x10; index1++ )
  64.             RemapTable[(index<<5) + index1] = (char) ((index - index/8 + index/16)<<4) +
  65.                                             (index1 - index1/8 + index1/16);
  66.     }    
  67.                             
  68.         srcPixMap = GetGWorldPixMap ( src );
  69.         dstPixMap = GetGWorldPixMap ( dst );
  70.         if ( LockPixels ( srcPixMap ) && LockPixels( dstPixMap) ) {    /* lock the pixmaps */
  71.         srcBaseAddr = (long *) GetPixBaseAddr ( srcPixMap );    /* get the address of the pixmap */
  72.         srcRowBytes = (**srcPixMap).rowBytes & 0x7fff;            /* get the row increment */
  73.         dstBaseAddr = (long *) GetPixBaseAddr ( dstPixMap );    /* get the address of the pixmap */
  74.         dstRowBytes = (**dstPixMap).rowBytes & 0x7fff;            /* get the row increment */
  75.         width = (**srcPixMap).bounds.right-(**srcPixMap).bounds.left;
  76.         height = (**srcPixMap).bounds.bottom-(**srcPixMap).bounds.top;
  77.  
  78.         mmuMode = true32b;
  79.         SwapMMUMode ( &mmuMode );                        /* set the MMU to 32-bit mode */
  80.         for ( row = 0; row < (height/4); row++ ) {            /* get each pixel in the pixmap */
  81.             srcAddr1 = srcBaseAddr;
  82.             srcAddr2 = (long *) ( (char *) srcAddr1 + srcRowBytes );
  83.             srcAddr3 = (long *) ( (char *) srcAddr2 + srcRowBytes );
  84.             srcAddr4 = (long *) ( (char *) srcAddr3 + srcRowBytes );
  85.             dstAddr = dstBaseAddr;
  86.             for ( column = 0; column < ((width+31)>>5); column++ ) {
  87.                 thirtyTwoPixels1 = *srcAddr1++;                /* get 4 longs of src */
  88.                 thirtyTwoPixels2 = *srcAddr2++;                /* get 4 longs of src */
  89.                 thirtyTwoPixels3 = *srcAddr3++;                /* get 4 longs of src */
  90.                 thirtyTwoPixels4 = *srcAddr4++;                /* get 4 longs of src */
  91.                 dstLong = 0;
  92. /* Do 8 bits of source */
  93.                 dstTenBits  = TranslationTable[thirtyTwoPixels1 & 0x000000FF];
  94.                 dstTenBits += TranslationTable[thirtyTwoPixels2 & 0x000000FF];
  95.                 dstTenBits += TranslationTable[thirtyTwoPixels3 & 0x000000FF];
  96.                 dstTenBits += TranslationTable[thirtyTwoPixels4 & 0x000000FF];
  97.                 dstChar =  RemapTable[dstTenBits];
  98.                 dstLong  = dstChar;
  99.  
  100. /* Do 2nd 8 bits of source */
  101.                 thirtyTwoPixels1 >>= 8;                        /* move to 2nd byte */
  102.                 thirtyTwoPixels2 >>= 8;                        /* move to 2nd byte */
  103.                 thirtyTwoPixels3 >>= 8;                        /* move to 2nd byte */
  104.                 thirtyTwoPixels4 >>= 8;                        /* move to 2nd byte */
  105.  
  106.                 dstTenBits  = TranslationTable[thirtyTwoPixels1 & 0x000000FF];
  107.                 dstTenBits += TranslationTable[thirtyTwoPixels2 & 0x000000FF];
  108.                 dstTenBits += TranslationTable[thirtyTwoPixels3 & 0x000000FF];
  109.                 dstTenBits += TranslationTable[thirtyTwoPixels4 & 0x000000FF];
  110.                 dstChar =  RemapTable[dstTenBits];
  111.                 dstLong  += (dstChar<<8);
  112. /* Do 3rd 8 bits of source */
  113.                 thirtyTwoPixels1 >>= 8;                        /* move to 3nd byte */
  114.                 thirtyTwoPixels2 >>= 8;    
  115.                 thirtyTwoPixels3 >>= 8;
  116.                 thirtyTwoPixels4 >>= 8;
  117.  
  118.                 dstTenBits  = TranslationTable[thirtyTwoPixels1 & 0x000000FF];
  119.                 dstTenBits += TranslationTable[thirtyTwoPixels2 & 0x000000FF];
  120.                 dstTenBits += TranslationTable[thirtyTwoPixels3 & 0x000000FF];
  121.                 dstTenBits += TranslationTable[thirtyTwoPixels4 & 0x000000FF];
  122.                 dstChar =  RemapTable[dstTenBits];
  123.                 dstLong  += (long) ((long)dstChar<<16);
  124. /* Do 4th 8 bits of source */
  125.                 thirtyTwoPixels1 >>= 8;                        /* move to 4th byte */
  126.                 thirtyTwoPixels2 >>= 8;    
  127.                 thirtyTwoPixels3 >>= 8;
  128.                 thirtyTwoPixels4 >>= 8;
  129.  
  130.                 dstTenBits  = TranslationTable[thirtyTwoPixels1 & 0x000000FF];
  131.                 dstTenBits += TranslationTable[thirtyTwoPixels2 & 0x000000FF];
  132.                 dstTenBits += TranslationTable[thirtyTwoPixels3 & 0x000000FF];
  133.                 dstTenBits += TranslationTable[thirtyTwoPixels4 & 0x000000FF];
  134.                 dstChar =  RemapTable[dstTenBits];
  135.                 dstLong  += (long)((long)dstChar<<24);
  136.  
  137.                 *dstAddr++ = dstLong;
  138.             }
  139.             srcBaseAddr = (long *) ( (char *) srcBaseAddr + 4 * srcRowBytes );    /* go to the next 4 rows */
  140.             dstBaseAddr = (long *) ( (char *) dstBaseAddr + dstRowBytes );    /* go to the next row */
  141.         }
  142.         SwapMMUMode ( &mmuMode );                        /* restore the previous MMU mode */
  143.         UnlockPixels ( srcPixMap );                        /* unlock the pixmap */
  144.         UnlockPixels ( dstPixMap );                        /* unlock the pixmap */
  145.     }
  146.  
  147. }